#include<stdio.h>
#include<string.h>
#include<assert.h>
//该方法：先找最小值，再对多的部分赋予'\0'
char* strncpy1(char* dest, char* src, int n){
	int length = strlen(src);
	int min = length > n ? n : length;
	for(int i = 0 ; i < min ; i++){
		dest[i] = src[i];
	}
	if(length < n){
		int offset = n - length;
		for(int i = 0 ; i < offset ; i++){			
			dest[min+i] = '\0';
		}
	}
	return dest;
}
//优化写法：先写主要逻辑，之后再把余下部分往上加，就可以写出这种优雅的代码
char* strncpy2(char* dest, char* src, int n){
	assert(dest != NULL && src != NULL);
	int length = n;
	int src_length = strlen(src);
	int offset = 0;
	if(src_length < n){
		length = src_length;
		offset = n - src_length;
	}
	while(length--){
		*dest++ = *src++;
	}
	while(offset--){
		*dest = '\0';
	}
	return dest;

}
int main(){
	char dest[20] = {"*******************"};
	char *src = "hello";

	strncpy2(dest, src, 10);
	puts(src);
	puts(dest);

	return 0;
}
